'''run like:
    nosetests -s <file>.py
'''

import os, sys, time, string
from vs_driver import *
from vim_driver import *

vs_7 = get_devenv_exe('7.1')
vs_8 = get_devenv_exe('8.0')

def test_vs7_csharp ():
    vs, vim = None, None
    try:
        # startup
        vs = VsDriver (vs_7, r'Vs7VimTestCs\Vs7VimTestCs.sln')
        vs.start ()
        vim = VimDriver ()
        vim.run_unique_gvim ('-R', '--noplugin')
        vim.remote_send (':source ../visual_studio.vim<cr>')
        # open files in vs, get files into vim
        filename1 = r'Vs7VimTestCs\Main\Main.cs'
        vs.open_file (filename1)
        assert files_same (filename1, vs.get_file())
        vim.remote_send (':call DTEGetFile()<cr>')
        time.sleep (0.5)
        assert files_same (filename1, vim.remote_expr ('expand("%:p")'))
        filename2 = r'Vs7VimTestCs\Dll\Dll.cs'
        vs.open_file (filename2)
        assert files_same (filename2, vs.get_file())
        vim.remote_send (':call DTEGetFile()<cr>')
        time.sleep (0.5)
        assert files_same (filename2, vim.remote_expr ('expand("%:p")'))
        # open files in 
        filename3 = 'test1.txt'
        vim.remote_send (':edit %s<cr>' % (filename3, ))
        time.sleep (0.5)
        assert files_same (filename3, vim.remote_expr ('expand("%:p")'))
        vim.remote_send (':call DTEPutFile()<cr>')
        time.sleep (0.5)
        assert files_same (filename3, vs.get_file())
        filename4 = 'test2.txt'
        vim.remote_send (':edit %s<cr>' % (filename4, ))
        time.sleep (0.5)
        assert files_same (filename4, vim.remote_expr ('expand("%:p")'))
        vim.remote_send (':call DTEPutFile()<cr>')
        time.sleep (0.5)
        assert files_same (filename4, vs.get_file())
    finally:
        if vs: vs.stop ()
        if vim: vim.stop ()
        print 'xx leaving test_vs7_csharp'

def files_same (f1, f2):
    # print 'xx files_same:'
    # print '    f1 (1):', f1
    # print '    f2 (1):', f2
    f1 = os.path.abspath (string.strip(str(f1)))
    f2 = os.path.abspath (string.strip(str(f2)))
    # print '    f1 (2):', f1, type(f1)
    # print '    f2 (2):', f2, type(f2)
    result = f1 == f2
    # print '    result:', result
    return result

# class SimpleTestCase (unittest.TestCase):
#     vs_driver = None
#     vim_driver = None
#
#     def setUp (self):
#         if not SimpleTestCase.vs_driver:
#             SimpleTestCase.vs = VsDriver (vs_7, r'Vs7VimTestCs\Vs7VimTestCs.sln')
#             SimpleTestCase.vs = VsDriver (vs_7, r'Vs7VimTestCs\Vs7VimTestCs.sln')
#
#     def test_simple (self):
#         vs = VsDriver (vs_7, r'Vs7VimTestCs\Vs7VimTestCs.sln')
#         vs.start ()
#         # self.assertEquals (0, 1)

if __name__ == '__main__':
    print __doc__
